package com.zhongxi.guoyuan.fragment;


import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.google.gson.Gson;
import com.zhongxi.guoyuan.R;
import com.zhongxi.guoyuan.adapter.PersonCenterAdapter;
import com.zhongxi.guoyuan.application.MyApplication;
import com.zhongxi.guoyuan.base.BaseFragment;
import com.zhongxi.guoyuan.entity.BaseResponse;
import com.zhongxi.guoyuan.entity.user.center.UserCenterResult;
import com.zhongxi.guoyuan.url.Url;
import com.zhongxi.guoyuan.utils.GlideRoundTransform;
import com.zhongxi.guoyuan.view.update.UpdateActivity;
import com.zhongxi.guoyuan.view.personalCenter.BeautifulStoreOrderActivity;
import com.zhongxi.guoyuan.view.personalCenter.BoxActivity;
import com.zhongxi.guoyuan.view.personalCenter.CoinOrderActivity;
import com.zhongxi.guoyuan.view.personalCenter.HasConsumeActivity;
import com.zhongxi.guoyuan.view.personalCenter.MyCollectionActivity;
import com.zhongxi.guoyuan.view.personalCenter.RewardCoinOrderActivity;
import com.zhongxi.guoyuan.view.personalCenter.SettingActivity;
import com.zhongxi.guoyuan.view.personalCenter.WithDrawCashActivity;
import com.zhongxi.guoyuan.view.shopCart.ShoppingCartActivity;
import com.zhongxi.guoyuan.view.user.LoginActivity;
import com.zhongxi.guoyuan.widget.goldshop.NoScrollGridView;

import java.util.LinkedList;
import java.util.List;

/**
 * A simple {@link Fragment} subclass.
 */
public class MainPersonFragment extends BaseFragment implements View.OnClickListener {

    private NoScrollGridView gvList;
    //    private int[] imgs = new int[]{R.mipmap.iv_parlor, R.mipmap.iv_gold_shop, R.mipmap.iv_coin,
//            R.mipmap.iv_person4, R.mipmap.iv_person5, R.mipmap.iv_person6, R.mipmap.iv_person7, R.mipmap.iv_person8, R.mipmap.iv_person9};
    private int[] imgs = new int[]{R.mipmap.iv_parlor, R.mipmap.iv_gold_shop, R.mipmap.iv_coin,
            R.mipmap.iv_person4, R.mipmap.iv_person5, R.mipmap.iv_person6, R.mipmap.iv_person8, R.mipmap.iv_person9};

    //    private String[] names = new String[]{"美容下单列表","返银订单列表","银币消费列表","我的收藏","购物车","设置","我的爱心互助金","提现助手", "已消费订单"};
    private String[] names = new String[]{"美容下单列表", "返银订单列表", "银币消费列表", "我的收藏", "购物车", "设置", "提现助手", "已消费订单"};
    private ImageView ivBox;

    //地图导航
    public static final String ROUTE_PLAN_NODE = "routePlanNode";
    public static List<Activity> activityList = new LinkedList<Activity>();
    private TextView tvUpdate;
    private TextView tvUserName;
    private TextView tvUserPhone;
    private TextView tvBonusPoint;
    private TextView tvIdentify;
    private ImageView ivUserHead;
    private TextView tvMoney;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_main_person, container, false);
    }

    @Override
    protected void initView() {
        super.initView();
        initTitle();
        tvTitle.setText("个人中心");
        ivBox = (ImageView) getView().findViewById(R.id.ivBox);
        tvMoney = (TextView) getView().findViewById(R.id.tvMoney);
        tvUpdate = (TextView) getView().findViewById(R.id.tvUpdate);
        tvUserName = (TextView) getView().findViewById(R.id.tvUserName);
        tvUserPhone = (TextView) getView().findViewById(R.id.tvUserPhone);
        ivUserHead = (ImageView) getView().findViewById(R.id.ivUserHead);
        tvBonusPoint = (TextView) getView().findViewById(R.id.tvBonusPoint);
        tvIdentify = (TextView) getView().findViewById(R.id.tvIdentify);
        gvList = (NoScrollGridView) getView().findViewById(R.id.gvList);
        PersonCenterAdapter personCenterAdapter = new PersonCenterAdapter(context, imgs, names);
        gvList.setAdapter(personCenterAdapter);
    }

    @Override
    protected void initData() {
        super.initData();
        getServer(Url.USER_CENTER, null, "", false, null, "get");
    }

    @Override
    protected void initEvent() {
        super.initEvent();
        ivBack.setOnClickListener(this);
        ivBox.setOnClickListener(this);
        tvUpdate.setOnClickListener(this);
        gvList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                switch (position) {
                    case 0:
                        startAct(BeautifulStoreOrderActivity.class);//美容院订单
                        break;
                    case 1:
                        startAct(RewardCoinOrderActivity.class);//返银订单列表
                        break;
                    case 2:
                        startAct(CoinOrderActivity.class);//银币消费
                        break;
                    case 3:
                        startAct(MyCollectionActivity.class);//收藏
                        break;
                    case 4:
                        startAct(ShoppingCartActivity.class);//购物车
                        break;
                    case 5:
                        startAct(SettingActivity.class);//设置
                        break;
//                    case 6:
//                        startAct(BeautifulStoreOrderActivity.class);//爱心互助金
//                        break;
                    case 6:
                        startAct(WithDrawCashActivity.class);//提现助手
                        break;
                    case 7:
                        startAct(HasConsumeActivity.class);//已消费订单
                        break;
                }
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.ivBack:
                getActivity().finish();
                break;
            case R.id.ivBox:
                startAct(BoxActivity.class);
                getActivity().finish();
                break;
            case R.id.tvUpdate:
                startAct(UpdateActivity.class);
                break;
        }
    }

    @Override
    protected void processSuccessResult(String response) {
        super.processSuccessResult(response);
        Gson gson = new Gson();
        try {
            UserCenterResult result = gson.fromJson(response, UserCenterResult.class);
            MyApplication.getInstance().setSpValue(result.getSafeobj());
            initViewData(result);
            SharedPreferences sharedPrefrences = getActivity().getSharedPreferences(Url.APP_SP, getActivity().MODE_PRIVATE);
            sharedPrefrences.edit().putString("rmb", result.getUserExt().getRmb()).commit();
            sharedPrefrences.edit().putString("silver", result.getUserExt().getSilver()).commit();
            sharedPrefrences.edit().putString("inner", result.getUserExt().getInnercurrency()).commit();
            sharedPrefrences.edit().putString("avatar", result.getUserInfo().getAvatar()).commit();
            if ("1".equals(result.getUserInfo().getIsprivilege())) {
                sharedPrefrences.edit().putBoolean("privilege", true).commit();
                Boolean a = MyApplication.getInstance().isPrivilege();
            }
            if ("1".equals(result.getUserInfo().getIsshareholders())) {
                sharedPrefrences.edit().putBoolean("shareHolder", true).commit();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void initViewData(UserCenterResult result) {
        try {
            if (null != result.getUserInfo().getShopname() && !"".equals(result.getUserInfo().getShopname())) {
                tvUserName.setText(result.getUserInfo().getShopname());
            } else {
                tvUserName.setText(R.string.app_name);
            }
            if (null != MyApplication.getInstance().getPhone() && !"".equals(MyApplication.getInstance().getPhone())) {
                tvUserPhone.setText(MyApplication.getInstance().parseSecutiryKey(result.getUserInfo().getMphone()));
            } else {
                tvUserPhone.setText("");
            }
            int point = 0;
            if (null != result.getRights() && result.getRights().size() > 0) {
                for (int i = 0; i < result.getRights().size(); i++) {
                    if (null != String.valueOf(result.getRights().get(i).getUsertype())) {
                        if (result.getRights().get(i).getUsertype() == 0) {  //股东
                            if (null != String.valueOf(result.getRights().get(i).getSharesnum()))
                                point += result.getRights().get(i).getSharesnum();
                        } else {                                                            //权益会员
                            if (null != String.valueOf(result.getRights().get(i).getSharesnum()))
                                point += result.getRights().get(i).getSharesnum();
                        }
                    }
                }
            }
            if (null != String.valueOf(point)) {
                tvBonusPoint.setText(String.valueOf(point));
            }else {
                tvBonusPoint.setText("0");
            }
            if (null != result.getUserInfo().getAvatar() && !"".equals(result.getUserInfo().getAvatar())) {
                Glide.with(context)
                        .load(Url.BASE_URL + result.getUserInfo().getAvatar())
                        .placeholder(R.mipmap.iv_eg)
                        .transform(new GlideRoundTransform(context, 20))
                        .into(ivUserHead);
            }
            String sb = "";
            if (null != result.getUserInfo().getIsprivilege()) {  //权益会员
                if ("1".equals(result.getUserInfo().getIsprivilege())) {
                    sb += "权益会员";
                    try {
                        if (null != result.getRights() && !"".equals(result.getRights())) {
                            if ("1".equals(result.getRights().get(0).getUsertype())) {
                                sb += "(" + result.getRights().get(0).getEtime() + ")";
                            } else {
                                sb += "(" + result.getRights().get(1).getEtime() + ")";
                            }
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    tvUpdate.setVisibility(View.GONE);
                }
            }
            if (null != sb && !"".equals(sb)) {
                tvIdentify.setText(sb.toString());
            } else {
                tvIdentify.setText("普通会员");
            }
            String money = "";
            if (null != result.getUserExt().getInnercurrency() && !"".equals(result.getUserExt().getInnercurrency())) {
                money += result.getUserExt().getInnercurrency() + "美币/";
            } else {
                money += "0美币/";
            }
            if (null != result.getUserExt().getSilver() && !"".equals(result.getUserExt().getSilver())) {
                money += result.getUserExt().getSilver() + "银币/";
            } else {
                money += "0银币/";
            }
            if (null != result.getUserExt().getRmb() && !"".equals(result.getUserExt())) {
                money += result.getUserExt().getRmb() + "元";
            } else {
                money += "0元";
            }
            tvMoney.setText(money);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void ProcessFailResult(String response) {
        super.ProcessFailResult(response);
        Gson gson = new Gson();
        BaseResponse result = gson.fromJson(response, BaseResponse.class);
        if (null != result.getSafeobj() && !"".equals(result.getSafeobj())) {
            MyApplication.getInstance().setSpValue(result.getSafeobj());
        }
        if (1001 == result.getCode()) {
            showShortToast("用户未登录，请重新登陆");
        }
        startAct(LoginActivity.class);
        Log.i("base", response);
    }
}
